package com.zjdiepu.www.biz.dh;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.InsuranceStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhInsuranceMapper;
import com.zjdiepu.www.dao.dh.DhInsuranceRenewalMapper;
import com.zjdiepu.www.dao.dh.FkInsureListMapper;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceService;
import com.zjdiepu.www.model.dh.dto.DhInsurance;
import com.zjdiepu.www.model.dh.dto.DhInsuranceRenewal;
import com.zjdiepu.www.model.dh.dto.FkInsureList;
import com.zjdiepu.www.model.dh.ext.DhInsuranceExt;

/**
 * 保险Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月17日 10时36分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhInsuranceServiceImpl extends BaseService implements IDhInsuranceService {
	public static final Logger logger = LoggerFactory.getLogger(DhInsuranceServiceImpl.class);

	@Autowired
	private DhInsuranceMapper dhInsuranceMapper;
	@Autowired
	private FkInsureListMapper fkInsureListMapper;
	@Autowired
	private DhInsuranceRenewalMapper dhInsuranceRenewalMapper;
	
	@Override
	public ApiResponse saveCancelInsurance(String renewalId, boolean isRenewal,
			AbstractSysUser currentUser) {
		if(isRenewal) {
			DhInsuranceRenewal dhInsuranceRenewal = new DhInsuranceRenewal();
			dhInsuranceRenewal.setId(renewalId);
			dhInsuranceRenewal.setStatusRenewal(InsuranceStatus.BU_BAO.getCode());
			dhInsuranceRenewalMapper.updateByPrimaryKeySelective(dhInsuranceRenewal);
		} else {
			FkInsureList fkInsureList = new FkInsureList();
			fkInsureList.setId(renewalId); //保单ID
			fkInsureList.setInsuranceStatus(InsuranceStatus.BU_BAO.getCode());
			fkInsureListMapper.updateByPrimaryKeySelective(fkInsureList);
		}
		return ApiResponse.success(true);
	}
	
	@Override
	public ApiResponse saveInsurance(DhInsurance dhInsurance, boolean isRenewal,
			AbstractSysUser currentUser) {
		String id = dhInsurance.getId();
		if(StringUtils.isBlank(id)) {
			dhInsurance.initInsertData(currentUser.getId(), currentUser.getName());
			dhInsuranceMapper.insert(dhInsurance);
		} else {
			dhInsurance.initUpdateData(currentUser.getId(), currentUser.getName());
			dhInsuranceMapper.updateByPrimaryKeySelective(dhInsurance);
		}
		if(isRenewal) {
			DhInsuranceRenewal dhInsuranceRenewal = new DhInsuranceRenewal();
			dhInsuranceRenewal.setId(dhInsurance.getRenewalId());
			dhInsuranceRenewal.setStatusRenewal(InsuranceStatus.YI_BAO.getCode());
			if(dhInsurance.getDateEnd()!=null && dhInsurance.getqDateEnd()!=null){
				Date dateEnd=dhInsurance.getDateEnd();
				Date qDateEnd=dhInsurance.getqDateEnd();
				dhInsuranceRenewal.setDateDue(dateEnd.getTime()>qDateEnd.getTime()?qDateEnd:dateEnd);
			}
			dhInsuranceRenewalMapper.updateByPrimaryKeySelective(dhInsuranceRenewal);
		} else {
			FkInsureList fkInsureList = new FkInsureList();
			fkInsureList.setId(dhInsurance.getRenewalId()); //保单ID
			fkInsureList.setInsuranceStatus(InsuranceStatus.YI_BAO.getCode());
			fkInsureListMapper.updateByPrimaryKeySelective(fkInsureList);
		}
		return ApiResponse.success(dhInsurance.getId());
	}
	
	@Override
	public void saveUpdateAndDelete(DhInsurance dhInsurance, OperateType operType) {
		switch (operType) {
		case add:
			dhInsurance.initInsertData(dhInsurance.getModifier(), dhInsurance.getModifierName());
			dhInsuranceMapper.insert(dhInsurance);
			break;
		case edit:
			dhInsurance.initUpdateData(dhInsurance.getModifier(), dhInsurance.getModifierName());
			// 影响的记录数
			int updateCount = dhInsuranceMapper.updateByPrimaryKeySelective(dhInsurance);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhInsurance.initDeleteData(dhInsurance.getModifier(), dhInsurance.getModifierName());
			// 影响的记录数
			int deleteCount = dhInsuranceMapper.updateByPrimaryKeySelective(dhInsurance);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhInsurance> selectByExt(DhInsuranceExt dhInsuranceExt) {
		return dhInsuranceMapper.selectByExt(dhInsuranceExt);
	}
	
	@Override
	public int countByExt(DhInsuranceExt dhInsuranceExt){
		return dhInsuranceMapper.countByExt(dhInsuranceExt);
	}
	
	@Override
	public DhInsurance selectById(String id) {
		return dhInsuranceMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhInsuranceMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhInsurance dhInsurance) {
		return dhInsuranceMapper.countRefined(dhInsurance);
	}

	@Override
	public DhInsurance selectRefined(DhInsurance dhInsurance) {
		return dhInsuranceMapper.selectRefined(dhInsurance);
	}
	
	@Override
	public int deletedRefined(DhInsurance dhInsurance) {
		return dhInsuranceMapper.deletedRefined(dhInsurance);
	}

	@Override
	public boolean repeat(DhInsurance dhInsurance) {
		int count = countRefined(dhInsurance);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhInsurance.getId())) {
			return true;
		}
		DhInsurance dbItem = selectRefined(dhInsurance);
		if(null == dbItem) {
			return false;
		}
		return !dhInsurance.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhInsuranceMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhInsurance> dhInsurances) {
		return dhInsuranceMapper.insertBatch(dhInsurances);
	}
}
